package models

import "time"
import "gorm.io/gorm"
import "go_part/utils"
//import "fmt"

type DeparkmentBasic struct{
	gorm.Model
	Name string
	DeparkmentID string
	CompanyID string
	Phone string `valid:"matches(^1[3-9]{1}\\d{9}$)"`
	Email string `valid:"email"`
	Identity string
	ClientIP string
	ClientPort string
	Salt string
	LoginTime time.Time
	isLogout bool
	DeviceInfo string
}

func (table *DeparkmentBasic) TableName() string {

	return "deparkment_basic"
}

func GetDeparkmentList() []*DeparkmentBasic { 
	data := make([]*DeparkmentBasic,10)
	utils.DB.Find(&data)
	// for _,v : = range data {
	// 	fmt.Println(v)
	// }
	return data
}

func FindDeparkmentBasicByName(name string,CompanyID string) DeparkmentBasic {
	user := DeparkmentBasic{}
	utils.DB.Where("name = ? and company_id = ?",name,CompanyID).Find(&user)
	return user
}

func FindDeparkmentBasicByID(id uint) DeparkmentBasic {
	user := DeparkmentBasic{}
	utils.DB.Where("id = ?",id).First(&user)
	return user
}

func FindDeparkmentBasicByIDAndCompany(id string,CompanyID string) DeparkmentBasic {
	user := DeparkmentBasic{}
	utils.DB.Where("deparkment_id = ? and company_id = ?",id,CompanyID).First(&user)
	return user
}

func FindDeparkmentByCompanyID(CompanyID string) []*DeparkmentBasic {
	user := make([]*DeparkmentBasic,100)
	utils.DB.Where("company_id = ?",CompanyID).Find(&user)
	return user
}

func CreateDeparkmentBasic(user DeparkmentBasic) *gorm.DB {
	
	return utils.DB.Create(&user)
}

func DeleteDeparkmentBasic(user DeparkmentBasic) *gorm.DB {
	
	return utils.DB.Delete(&user)
}

func UpdateDeparkmentBasic(user DeparkmentBasic) *gorm.DB {
	
	return utils.DB.Model(&user).Updates(DeparkmentBasic{Name: user.Name, DeviceInfo: user.DeviceInfo})
}